﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DTO;
using System.Text.RegularExpressions;

namespace GaraOto
{
    public partial class frmLotManager : DevExpress.XtraEditors.XtraUserControl
    {
        FloorBLL floorBLL = new FloorBLL();
        LotBLL lotBLL = new LotBLL();
        string number = "^[1-9][0-9]*$";
        public frmLotManager()
        {
            InitializeComponent();
            BindingDataFloor();
            FillDataComboboxFloor();
            BindingDataLot();
        }

        public void BindingDataFloor()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = floorBLL.SelectAll();
            bnvFloor.BindingSource = bs;
            gcFloors.DataSource = bs;

            btnEditFloor.Enabled = false;
        }

        public void BindingDataLot()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = lotBLL.SelectAll();
            bnvLot.BindingSource = bs;
            gcLots.DataSource = bs;
        }
        public void ResetFloor()
        {
            teFloorName.EditValue = null;
            cbeFloorStatus.SelectedIndex = 0;

            btnAddFloor.Enabled = true;
            btnEditFloor.Enabled = false;
            teFloorName.Enabled = true;
        }

        public void ResetLot()
        {
            cbeFloor.SelectedIndex = 0;
            teNumberLot.EditValue = null;
            cbeLotStatus.SelectedIndex = 0;

            btnAddLot.Enabled = true;
            btnEditLot.Enabled = false;
            cbeFloor.Enabled = true;
            teNumberLot.Enabled = true;
        }

        public void FillDataComboboxFloor()
        {
            List<FloorDTO> list = floorBLL.FillDataCombobox();
            cbeFloor.Properties.Items.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                cbeFloor.Properties.Items.Add(list[i].NumberFloor);
            }
            cbeFloor.SelectedIndex = 0;

        }

        public int getValueComboboxFloor(int NumberFloor)
        {
            List<FloorDTO> list = floorBLL.FillDataCombobox();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].NumberFloor.Equals(NumberFloor))
                {
                    return list[i].FloorID;
                }
            }
            return -1;
        }

        private void btnAddFloor_Click(object sender, EventArgs e)
        {
            FloorDTO floorDTO = new FloorDTO();
            Regex regex = new Regex(number);
            if (teFloorName.Text != "")
            {
                if (regex.IsMatch(teFloorName.Text.Trim()))
                    if (!floorBLL.CheckFloorExist(Convert.ToInt32(teFloorName.Text)))
                    {
                        floorDTO.NumberFloor = Convert.ToInt32(teFloorName.Text);
                        floorDTO.Status = cbeFloorStatus.SelectedItem.ToString();

                        string message = floorBLL.Insert(floorDTO);
                        if (message.Equals(MessageAction.Success))
                        {
                            MessageBox.Show("Thêm mới tầng để xe thành công");
                            BindingDataFloor();
                            ResetFloor();
                        }
                        else
                        {
                            MessageBox.Show(message);
                        }
                    }

                    else
                        MessageBox.Show("Tầng này đã tồn tại");
                else
                    MessageBox.Show("Dữ liệu tầng nhập không hợp lệ");
            }
            else
            {
                MessageBox.Show("Bạn hãy nhập dữ liệu tầng");
            }

        }

        private void btnEditFloor_Click(object sender, EventArgs e)
        {
            FloorDTO floorDTO = new FloorDTO();
            floorDTO.FloorID= Convert.ToInt32(gvFloors.GetRowCellValue(gvFloors.GetSelectedRows()[0],gcFloorID).ToString());
            floorDTO.NumberFloor = Convert.ToInt32(teFloorName.Text);
            floorDTO.Status = cbeFloorStatus.SelectedItem.ToString();

            string message = floorBLL.Update(floorDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Cập nhật thông tin tầng để xe thành công");
                BindingDataFloor();
                ResetFloor();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void gvFloors_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            teFloorName.Enabled = false;
            if (gvFloors.GetRowCellValue(gvFloors.GetSelectedRows()[0], gcFloorNameFloor) != null)
                teFloorName.Text = gvFloors.GetRowCellValue(gvFloors.GetSelectedRows()[0], gcFloorNameFloor).ToString();
            cbeFloorStatus.SelectedItem = gvFloors.GetRowCellValue(gvFloors.GetSelectedRows()[0], gcStatusFloor).ToString();

            btnAddFloor.Enabled = false;
            btnEditFloor.Enabled = true;
        }

        private void btnResetFloor_Click(object sender, EventArgs e)
        {
            ResetFloor();
        }

        private void btnResetLot_Click(object sender, EventArgs e)
        {
            ResetLot();
        }

        private void btnAddLot_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(number);
            LotDTO lotDTO = new LotDTO();
            if (!teNumberLot.Text.Equals(""))
            {
                if (regex.IsMatch(teNumberLot.Text.Trim()))
                {
                    if (!lotBLL.CheckLotExist(Convert.ToInt32(cbeFloor.SelectedItem), Convert.ToInt32(teNumberLot.Text)))
                    {
                        lotDTO.NumberLot = Convert.ToInt32(teNumberLot.Text);
                        lotDTO.FloorID = getValueComboboxFloor(Convert.ToInt32(cbeFloor.SelectedItem));
                        lotDTO.Status = cbeLotStatus.SelectedItem.ToString();

                        string message = lotBLL.Insert(lotDTO);
                        if (message.Equals(MessageAction.Success))
                        {
                            MessageBox.Show("Thêm mới chổ để xe thành công");
                            BindingDataLot();
                            ResetLot();
                        }
                        else
                        {
                            MessageBox.Show(message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Chổ để xe này đã tồn tại");
                    }
                }
                else
                {
                    MessageBox.Show("Số thứ tự bạn nhập không hợp lệ");
                }
            }
            else
            {
                MessageBox.Show("Bạn hãy nhập số thứ tự");
            }
        }

        private void btnEditLot_Click(object sender, EventArgs e)
        {            
            LotDTO lotDTO = new LotDTO();
            lotDTO.LotID = gvLots.GetRowCellValue(gvLots.GetSelectedRows()[0],gcLotID).ToString();
            lotDTO.NumberLot = Convert.ToInt32(teNumberLot.Text);
            lotDTO.FloorID = getValueComboboxFloor(Convert.ToInt32(cbeFloor.SelectedItem));
            lotDTO.Status = cbeLotStatus.SelectedItem.ToString();

            string message = lotBLL.Update(lotDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Cập nhật trạng thái chổ để xe thành công");
                BindingDataLot();
                ResetLot();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void gvLots_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            cbeFloor.Enabled = false;
            teNumberLot.Enabled = false;
            cbeFloor.SelectedItem = gvLots.GetRowCellValue(gvLots.GetSelectedRows()[0], gcNumberFloor).ToString();
            teNumberLot.Text = gvLots.GetRowCellValue(gvLots.GetSelectedRows()[0], gcNumberLot).ToString();
            cbeLotStatus.SelectedItem = gvLots.GetRowCellValue(gvLots.GetSelectedRows()[0], gcStatus).ToString();

            btnAddLot.Enabled = false;
            btnEditLot.Enabled = true;
        }
    }
}
